using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Windows.Forms;

namespace THSLib
{
    public class ADConfigValueUtility
    {
        #region constant for ADConfigValue
        public const string ADConfigValueAll = "All";
        public const string ADConfigValueSellPrice = "SellPrice";
        public const string ADConfigValueBuyPrice = "BuyPrice";
        public const string ADConfigValuePaymentMethod = "PaymentMethod";
        public const string ADConfigValueCustomerType = "CustomerType";
        public const string ADConfigValueCustomerInvoiceType = "CustomerInvoiceType";
        public const string ADConfigValueCustomerOrderType = "CustomerOrderType";
        public const string ADConfigValueSupplierType = "SupplierType";
        public const string ADConfigValueProductCostingMethod = "ProductCostingMethod";
        public const string ADConfigValueProductType = "ProductType";
        public const string ADConfigValueStockType = "StockType";
        public const string ADConfigValueContactType = "ContactType";
        public const string ADConfigValueSellOrderItemType = "SellOrderItemType";
        public const string ADConfigValueShipmentType = "ShipmentType";
        public const string ADConfigValueReceiptType = "ReceiptType";

        
        public const string ADConfigValuePOSType = "POSType";
        public const string ADConfigValuePOSStatus = "POSStatus";
        public const string ADConfigValueTransferType = "TransferType";
        
        public const string ADConfigValueLayBy = "LayBy";
        public const string ADConfigValueWorkingShift = "EmployeeWorkingShift";
        public const string ADConfigValueCreditNote = "CreditNote";

       
        public const string ADConfigValueRegionState = "AddressStateProvince";
        public const string ADConfigValueRegionPostCode = "AddressPostalCode";
        #endregion     

        #region ContactType Constant
        public const string cstContactTypeInvoice = "Invoice";
        public const string cstContactTypeProposal = "Proposal";
        public const string cstContactTypeSaleOrder = "SaleOrder";
       
        public const string cstContactTypePurchaseOrder = "PurchaseOrder";
        public const string cstContactTypeDelivery = "Delivery";
        public const string cstContactTypeReceipt = "Receipt";
        public const string cstContactTypeSaleReturn = "SaleReturn";
        public const string cstContactTypeCreditNote = "CreditNote";
        public const string cstContactTypeInvoiceCopy = "InvoiceCopy";
        public const string cstContactTypeProposalCopy = "ProposalCopy";
        public const string cstContactTypeSaleOrderCopy = "SaleOrderCopy";
        public const string cstContactTypeDeliveryCopy = "DeliveryCopy";
        public const string cstContactTypeReceiptCopy = "ReceiptCopy";
        public const string cstContactTypeSaleReturnCopy = "SaleReturnCopy";
        public const string cstContactTypeCreditNoteCopy = "CreditNoteCopy";
        #endregion
        
        #region Constant HistoryAction
        public const string cstHistoryActionCreate = "Create";
        public const string cstHistoryActionChange = "Change";
        public const string cstHistoryActionGenerate = "Generate";
        #endregion

        #region Constant for Proposal
        #region ProposalType Constant
        public const string cstProposalTypeManual = "Manual";
        #endregion

        #region ProposalStatus Constant
        public const string cstProposalStatusNew = "New";
        public const string cstProposalStatusIncomplete = "Incomplete";
        public const string cstProposalStatusComplete = "Complete";
        #endregion

        #region ProposalRelate Constant
        public const string cstProposalRelateManual = "Manual";
        public const string cstProposalRelateSellOrder = "SellOrder";
        #endregion        
        #endregion

        #region Constant for Commission

        #region CommissionType Constant
        public const string cstCommissionTypeManual = "Manual";
        public const string cstCommissionTypeFromSellOrder = "FromSellOrder";
        #endregion

        #region CommissionStatus Constant
        public const string cstCommissionStatusNew = "New";
        public const string cstCommissionStatusIncomplete = "Incomplete";
        public const string cstCommissionStatusComplete = "Complete";
        #endregion

        #region CommissionRelate Constant
        public const string cstCommissionRelateManual = "Manual";
        public const string cstCommissionRelateSellOrder = "SellOrder";
        public const string cstCommissionRelateShipment = "Shipment";
        #endregion

        #endregion

        #region Constant for SellReturn

        #region SellReturnType Constant
        public const string cstSellReturnTypeManual = "Manual";
        public const string cstSellReturnTypeFromInvoice = "FromInvoice";
        public const string cstSellReturnTypeFromCreditNote = "FromCreditNote";
        #endregion

        #region SellReturnStatus Constant
        public const string cstSellReturnStatusNew = "New";
        public const string cstSellReturnStatusIncomplete = "Incomplete";
        public const string cstSellReturnStatusComplete = "Complete";
        #endregion

        #region SellReturnRelate Constant
        public const string cstSellReturnRelateManual = "Manual";
        public const string cstSellReturnRelateInvoice = "Invoice";
        public const string cstSellReturnRelateCreditNote = "CreditNote";
        public const string cstSellReturnRelateReceipt = "Receipt";
        #endregion
        #endregion

        #region Constant for CreditNote
        #region CreditNoteType constant
        public const string cstCreditNoteTypeManual = "Manual";
        public const string cstCreditNoteTypeFromSellReturn = "FromSellReturn";
        public const string cstCreditNoteTypeFromInvoice = "FromInvoice";
        public const string cstCreditNoteTypeFromInvoiceAffectStock = "FromInvoiceAffectStock";
        #endregion

        #region CreditNoteStatus constant
        public const string cstCreditNoteStatusNew = "New";
        public const string cstCreditNoteStatusIncomplete = "Incomplete";
        public const string cstCreditNoteStatusComplete = "Complete";
        #endregion

        #region CreditNoteRelate Constant
        public const string cstCreditNoteRelateManual = "Manual";
        public const string cstCreditNoteRelateReceipt = "Receipt";
        public const string cstCreditNoteRelateInvoice = "Invoice";
        #endregion        
        #endregion

        #region Constant for Transaction Type
        public const string cstTransactionTypeProposal = "Proposal";
        public const string cstTransactionTypeSellOrder = "SellOrder";
        public const string cstTransactionTypeCommission = "Commission";
        public const string cstTransactionTypeShipment = "Shipment";
        public const string cstTransactionTypeInvoice = "Invoice";
        public const string cstTransactionTypePurchaseOrder = "PurchaseOrder";
        public const string cstTransactionTypeReceipt = "Receipt";
        public const string cstTransactionTypeSellReturn = "SellReturn";
        public const string cstTransactionTypeCreditNote = "CreditNote";
        public const string cstTransactionTypeTransfer = "Transfer";
        public const string cstTransactionTypeTransferStockIn = "TransferStockIn";
        public const string cstTransactionTypeTransferStockOut = "TransferStockOut";
        public const string cstTransactionTypeZerlegung = "Zerlegung";
        
        public const string cstTransactionTypeRezeptur = "Rezeptur";
        
        #endregion        

        #region Constant for SupplierType
        public const string cstStatusActiv = "StatusActiv";
        public const string cstStatusInActiv = "StatusInActiv";
        #endregion

        #region Constant ItemType
        public const string cstItemTypeProduct = "Product";
        public const string cstItemTypeText = "Text";
        #endregion

        #region Constant for open document
        public const string cstOpenDocumentTypeSaleReceipt = "SaleReceipt";
        public const string cstOpenDocumentTypeGoodsRefund = "GoodsRefund";
        public const string cstOpenDocumentTypeLayBy = "LayBy";

        public const string cstOpenDocumentStatusNew = "New";
        public const string cstOpenDocumentStatusCancel = "Cancel";
        public const string cstOpenDocumentStatusComplete = "Complete";
        #endregion

        #region Constant for payment method
        public const String cstPaymentMethodCash = "Cash";
        public const String cstPaymentMethodEFTPOS = "EFTPOS";
        public const String cstPaymentMethodCheck = "Check";
        public const String cstPaymentMethodCreditCard = "CreditCard";
        public const String cstPaymentMethodAccount = "Account";
        public const String cstPaymentMethodCreditNote = "CreditNote";
        public const String cstPaymentMethodGiftVoucher = "GiftVoucher";
        #endregion

        public static DataSet glbdsConfigValues = new DataSet();

        public static readonly String ADConfigValueTable = "ADConfigValues";

        public static void UpdateValueToADConfigValueTable(String strKey, String strText)
        {
            try
            {
                String strUpdateQuery = String.Format("UPDATE [ADConfigValues] SET [ADConfigText]='{0}' WHERE [ADConfigKey]='{1}'", strText, strKey);
                THSDbUtil.ExecuteQuery(strUpdateQuery);
            }
            catch (Exception)
            {
                MessageBox.Show("#Message# Exception");
            }
        }
    
        public static DataTable InitConfigValueTableStructure(String strConfigKey)
        {
            DataTable tbl = new DataTable(strConfigKey);
            DataColumn columnKey = new DataColumn();
            DataColumn[] Keys = new DataColumn[1];
            columnKey.ColumnName = "Key";
            columnKey.DataType = typeof(string);

            tbl.Columns.Add(columnKey);
            Keys[0] = columnKey;

            DataColumn columnKeyValue = new DataColumn();
            columnKeyValue.ColumnName = "Value";
            columnKeyValue.DataType = typeof(string);
            tbl.Columns.Add(columnKeyValue);


            DataColumn columnText = new DataColumn();
            columnText.ColumnName = "Text";
            columnText.DataType = typeof(string);

            tbl.Columns.Add(columnText);
            tbl.PrimaryKey = Keys;
 
            return tbl;
        }

        //public static DataTable InitConfigValueTable(String strKey)
        //{
        //    DataTable tbl = InitConfigValueTableStructure(strKey);
        //    string strColumnText = "ADConfigText";
        //    String strColumnKey = "ADConfigKey";
        //    String strColumnKeyValue = "ADConfigKeyValue";
        //    if (strKey == ADConfigValueAll)
        //    {
        //        strKey = string.Empty;
        //    }
        //    THSDbUtil dbUtil = new THSDbUtil();
        //    DataSet dsConfigs = dbUtil.GetDataFromTableNameWithTableColumnLikeColumnValue(ADConfigValueTable, strColumnKey, strKey);

        //    if (dsConfigs.Tables.Count > 0)
        //    {
        //        int i = dsConfigs.Tables[0].Rows.Count;
        //        foreach (DataRow rowColumn in dsConfigs.Tables[0].Rows)
        //        {
        //            String strColKey = rowColumn[strColumnKey].ToString();
        //            String strColKeyValue = rowColumn[strColumnKeyValue].ToString();
        //            String strColText = rowColumn[strColumnText].ToString();
        //            if (strColKey != null)
        //            {
        //                tbl.Rows.Add(new object[3] { strColKey,strColKeyValue, strColText});
        //            }
        //        }
        //    }

        //    return tbl;
        //}

        public static DataTable InitConfigValueTable(String strADConfigValueGroup)
        {
            DataTable tbl = InitConfigValueTableStructure(strADConfigValueGroup);
            string strColumnText = "ADConfigText";
            String strColumnKey = "ADConfigKey";
            String strColumnKeyValue = "ADConfigKeyValue";
            if (strADConfigValueGroup == ADConfigValueAll)
            {
                strADConfigValueGroup = string.Empty;
            }
            THSDbUtil dbUtil = new THSDbUtil();
            DataSet dsADConfigValues = new ADConfigValuesController().GetADConfigValuesByGroup(strADConfigValueGroup);

            if (dsADConfigValues.Tables.Count > 0)
            {
                int i = dsADConfigValues.Tables[0].Rows.Count;
                foreach (DataRow rowColumn in dsADConfigValues.Tables[0].Rows)
                {
                    String strColKey = rowColumn[strColumnKey].ToString();
                    String strColKeyValue = rowColumn[strColumnKeyValue].ToString();
                    String strColText = rowColumn[strColumnText].ToString();
                    if (strColKey != null)
                    {
                        tbl.Rows.Add(new object[3] { strColKey, strColKeyValue, strColText });
                    }
                }
            }

            return tbl;
        }
        

        public static String GetTextFromKey(String strKey,String strConfigValueTableName)
        {
            foreach (DataRow row in glbdsConfigValues.Tables[strConfigValueTableName].Rows)
            {
                if (row["Key"].ToString() == strKey)
                    return row["Text"].ToString();
            }
            return String.Empty;
        }


        public static String GetTextFromKey(String strKey)
        {
            foreach (DataTable tblConfigValues in glbdsConfigValues.Tables)
            {
                foreach (DataRow row in tblConfigValues.Rows)
                {
                    if (row["Key"].ToString() == strKey)
                        return row["Text"].ToString();
                }
            }

            return String.Empty;
            //foreach (DataRow row in glbdsConfigValues.Tables[ADConfigValueAll].Rows)
            //{
            //    if (row["Key"].ToString() == strKey)
            //        return row["Text"].ToString();
            //}
            //return String.Empty;
        }

        public static void InitGlobalConfigValueTables()
        {
            glbdsConfigValues.Tables.Clear();
            glbdsConfigValues.Clear();

            ADConfigValuesController objADConfigValuesController = new ADConfigValuesController();
            List<String> lstADConfigValueGroups = objADConfigValuesController.GetADConfigValueGroups();
            foreach (String strADConfigValueGroup in lstADConfigValueGroups)
            {
                if (!String.IsNullOrEmpty(strADConfigValueGroup))
                {
                    DataTable tbl = InitConfigValueTable(strADConfigValueGroup);
                    glbdsConfigValues.Tables.Add(tbl);

                    DataTable tblSearch = InitConfigValueTable(strADConfigValueGroup);
                    tblSearch.TableName = strADConfigValueGroup + "Search";
                    
                    DataRow dummyRow = tblSearch.NewRow();
                    dummyRow["Key"] = String.Empty;
                    dummyRow["Value"] = String.Empty;
                    dummyRow["Text"] = String.Empty;
                    tblSearch.Rows.InsertAt(dummyRow, 0);
                    glbdsConfigValues.Tables.Add(tblSearch);
                }
            }
        }        

        
    }
}



